﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using fanfu.Common;
using fanfu.Model;
using System.Data.SqlClient;

namespace fanfu.Business
{
    public class UploadFileRule
    {
        //----------后台调用------------

        #region 获取所有文件信息
        /// <summary>
        /// 获取所有文件信息
        /// </summary>
        public DataTable GetAllFileInfo()
        {
            string sqlString = "SELECT * FROM UploadFile ORDER BY CreateTime DESC";
            DataTable dt = SqlHelper.GetDataTable(sqlString);
            return dt;
        }
        #endregion

        #region 获取指定ID的文件信息
        /// <summary>
        /// 获取指定ID的文件信息
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public UploadFileModel GetUploadFileByID(string ID)
        {
            string sqlString = "SELECT TOP 1 * FROM UploadFile WHERE ID=@ID";
            SqlParameter para = new SqlParameter("@ID", ID);
            DataTable dt = SqlHelper.GetDataTable(sqlString, para);
            if (dt.Rows.Count == 1)
                return new UploadFileModel(dt.Rows[0]);
            else
                return null;
        }
        #endregion

        #region 删除指定ID的文件信息
        /// <summary>
        /// 删除指定ID的文件信息
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public int DeleteUploadFile(string ID)
        {
            string sqlString = string.Format("UPDATE UploadFile SET Flag={0} WHERE ID={1}", (int)UploadFileFlag.delete, ID);
            return SqlHelper.ExecuteCommand(sqlString);
        }
        #endregion

        #region 文件类型枚举值转换
        /// <summary>
        /// 文件类型枚举值转换
        /// </summary>
        public string GetFileType(object objFileType)
        {
            string strResult = string.Empty;
            int flag = Convert.ToInt32(objFileType);
            switch (flag)
            {
                case (int)UploadFileType.imageType: strResult = "图片"; break;
                case (int)UploadFileType.rarType: strResult = "压缩包"; break;
                case (int)UploadFileType.otherType: strResult = "其它"; break;
            }
            return strResult;
        }
        #endregion

        //----------前台调用------------

        #region 根据相册ID返回照片列表
        /// <summary>
        /// 根据相册ID返回照片列表
        /// </summary>
        /// <param name="parentId">相册ID</param>
        /// <returns></returns>
        public DataTable GetPhotoListByParentId(string parentId)
        {
            string sqlString = string.Format("SELECT * FROM UploadFile WHERE ParentId={0} AND FileType={1} AND Flag={2} ORDER BY CreateTime DESC", parentId, (int)UploadFileType.imageType, (int)UploadFileFlag.normal);
            return SqlHelper.GetDataTable(sqlString);
        }
        #endregion
    }
}
